﻿using EA.BA;
using EA.Common;
using EA.DTO;
using EAWebAdmin.template;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EAAdmin
{
    public partial class EditMember : WebPage
    {
        override
        protected void Load(object sender, EventArgs e)
        {
            if (Request["id"] != null && !"".Equals(Request["id"]) && StringUtil.IsPositiveNumber(Request["id"]))
            {
                MemberBA memberBA = new MemberBA();
                int ID = Convert.ToInt32(Request["id"]);
                MemberTO member = memberBA.GetMemberByID(ID);
                Attributes.Add("member", member);
            }
        }

        public void Update(HttpRequest request, HttpResponse response)
        {

            if (Request["id"] == null || Request["id"].Equals("") || !StringUtil.IsPositiveNumber(Request["id"]))
            {
                this.error("Member is invalid");
                this.isLoad = true;
                return;
            }
            MemberBA memberBA = new MemberBA();
            int ID = Convert.ToInt32(Request["id"]);
            MemberTO memberTO = memberBA.GetMemberByID(ID);
            if (memberTO == null)
            {
                this.error("Member is invalid");
                this.isLoad = true;
                return;
            }

            String pUserName = Request.Form["pUserName"];
            String pPassword = Request.Form["pPassword"];
            String pFullName = Request.Form["pFullName"];
            int pRole = Convert.ToInt32(Request.Form["pRole"]);
            if (pUserName != null && pUserName != null && pFullName != null)
            {
                Boolean temp = true;
                String err = "";
                if ("".Equals(pUserName))
                {
                    err += "User name can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pPassword))
                {
                    err += "Password can not be empty<br>";
                    temp = false;
                }
                if ("".Equals(pFullName))
                {
                    err += "Full name can not be empty<br>";
                    temp = false;
                }
                if (temp)
                {
                    memberTO.FullName = pFullName;
                    memberTO.Password = Md5.md5(pPassword);
                    memberTO.Role = pRole;
                    memberTO.UserName = pUserName;
                    Boolean stt = false;
                    stt = memberBA.UpdateMember(memberTO);
                    if (stt == true)
                    {
                        Attributes.Add("success", "Update success");
                        Attributes.Add("member", memberTO);
                        return;
                    }
                    else
                    {
                        Attributes.Add("err", "Update faulty");
                        return;
                    }
                }
                else
                {
                    Attributes.Add("err", err);
                    Attributes.Add("member", memberTO);
                }
            }
        }
    }
}